<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的订单</title>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }
        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
            padding: 20px 0;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            z-index: 1000;
        }
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header-title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .header-title::before {
            content: "🏨";
            font-size: 28px;
        }
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
            background: rgba(255, 255, 255, 0.8);
            padding: 10px 20px;
            border-radius: 25px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .user-avatar {
            width: 40px;
            height: 40px;
            background: linear-gradient(45deg, #667eea, #764ba2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-weight: bold;
            font-size: 18px;
        }
        .user-name {
            font-weight: 500;
            color: #333;
        }
        .user-action-btn {
            background: #f5f5f5;
            border: none;
            color: #5a3ec8;
            padding: 6px 16px;
            border-radius: 16px;
            margin-left: 8px;
            font-size: 14px;
            cursor: pointer;
            transition: background 0.2s, color 0.2s;
            text-decoration: none;
            display: inline-block;
        }
        .user-action-btn:hover {
            background: #e0e7ff;
            color: #333;
        }
        .main-content {
            max-width: 900px;
            margin: 120px auto 40px;
            padding: 0 20px;
        }
        .orders-title {
            font-size: 28px;
            font-weight: 600;
            color: #333;
            margin-bottom: 30px;
            text-align: center;
        }
        .orders-list {
            display: flex;
            flex-direction: column;
            gap: 24px;
        }
        .order-card {
            background: rgba(255,255,255,0.95);
            border-radius: 16px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.08);
            padding: 24px 32px;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .order-card .order-header {
            font-size: 18px;
            font-weight: 500;
            color: #5a3ec8;
        }
        .order-card .order-info {
            font-size: 15px;
            color: #555;
        }
        .order-card .order-status {
            font-size: 14px;
            color: #e67e22;
            margin-top: 6px;
        }
        .order-actions {
            margin-top: 10px;
            display: flex;
            gap: 10px;
        }
        .review-btn {
            background: #5a3ec8;
            color: white;
            border: none;
            padding: 6px 12px;
            border-radius: 12px;
            font-size: 12px;
            cursor: pointer;
            transition: background 0.2s;
        }
        .review-btn:hover {
            background: #764ba2;
        }
        .review-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
        }
        .review-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 1001;
        }
        .review-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 30px;
            border-radius: 16px;
            width: 400px;
            max-width: 90%;
        }
        .review-title {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 20px;
            text-align: center;
        }
        .star-rating {
            display: flex;
            justify-content: center;
            gap: 8px;
            margin-bottom: 20px;
        }
        .star {
            font-size: 28px;
            cursor: pointer;
            color: #ddd;
            transition: all 0.3s ease;
            position: relative;
            user-select: none;
        }
        .star:hover {
            color: #ffd700;
            transform: scale(1.1);
        }
        .star.active {
            color: #ffd700;
            text-shadow: 0 0 10px rgba(255, 0, 0, 0.5);
        }
        .star::before {
            content: "★";
            position: absolute;
            top: 0;
            left: 0;
            opacity: 0;
            transform: scale(0.8);
            transition: all 0.2s ease;
        }
        .star.active::before {
            opacity: 1;
            transform: scale(1);
        }
        .review-text {
            width: 100%;
            height: 100px;
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 10px;
            resize: vertical;
            margin-bottom: 20px;
        }
        .review-buttons {
            display: flex;
            gap: 10px;
            justify-content: center;
        }
        .review-submit, .review-cancel {
            padding: 8px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
        }
        .review-submit {
            background: #5a3ec8;
            color: white;
        }
        .review-cancel {
            background: #f5f5f5;
            color: #333;
        }
        .order-review {
            margin-top: 10px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #5a3ec8;
        }
        .review-stars {
            color: #ffd700;
            font-size: 18px;
            margin-bottom: 8px;
            display: flex;
            gap: 2px;
        }
        .review-stars .star-filled {
            color: #ffd700;
            text-shadow: 0 5px rgba(255, 0, 0.5);
        }
        .review-stars .star-empty {
            color: #ddd;
        }
        .review-content-text {
            color: #666;
            font-size: 14px;
        }
        .no-orders {
            text-align: center;
            color: #888;
            font-size: 18px;
            margin-top: 40px;
        }
        .back-home-btn {
            background: #f5f5f5;
            border: none;
            color: #5a3ec8;
            padding: 6px 18px;
            border-radius: 16px;
            font-size: 15px;
            cursor: pointer;
            margin-right: 18px;
            transition: background 0.2s, color 0.2s;
        }
        .back-home-btn:hover {
            background: #e0e7ff;
            color: #333;
        }
        @media (max-width: 768px) {
            .main-content {
                margin: 100px 0 20px 0;
                padding: 0 5px;
            }
            .order-card {
                padding: 16px 8px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            
            <div class="header-title">酒店管理系统</div>
            <!-- 用户信息栏示例 -->
            <div class="user-info">
                <div class="user-avatar">U</div>
                <div class="user-name">用户：<span id="userName">用户</span></div>
                <a href="index.html" class="user-action-btn">返回首页</a>
                <a href="reviews.html" class="user-action-btn">我的评价</a>
                <button id="logoutBtn" class="user-action-btn" style="margin-left:5px;">退出登录</button>
            </div>
        </div>
    </div>
    <div class="main-content">
        <div class="orders-title">我的订单</div>
        <div id="ordersList" class="orders-list"></div>
        <div id="noOrders" class="no-orders" style="display:none;">暂无订单</div>
    </div>
    
    <!-- 评价对话框 -->
    <div id="reviewModal" class="review-modal">
        <div class="review-content">
            <div class="review-title">评价订单</div>
            <div class="star-rating">
                <span class="star" data-rating="1" onclick="setRating(1)">★</span>
                <span class="star" data-rating="2" onclick="setRating(2)">★</span>
                <span class="star" data-rating="3" onclick="setRating(3)">★</span>
                <span class="star" data-rating="4" onclick="setRating(4)">★</span>
                <span class="star" data-rating="5" onclick="setRating(5)">★</span>
            </div>
            <textarea id="reviewText" class="review-text" placeholder="请输入您的评价内容..."></textarea>
            <div class="review-buttons">
                <button class="review-submit" onclick="submitReview()">提交评价</button>
                <button class="review-cancel" onclick="closeReviewModal()">取消</button>
            </div>
        </div>
    </div>
    
    <script>
        // 显示用户名
        document.getElementById('userName').innerText = sessionStorage.getItem('adminName') || '用户';
        // 退出登录按钮事件
        $(document).on('click', '#logoutBtn', function() {
            if(confirm('确定要退出登录吗？')) {
                sessionStorage.removeItem('adminName');
                sessionStorage.removeItem('userId');
                window.location.href = 'login.html';
            }
        });
        // 检查登录
        var userId = sessionStorage.getItem('userId');
        if (!userId) {
            window.location.href = 'login.html';
        }
        // 获取并展示订单
        function renderOrders(orders) {
            var $list = $('#ordersList');
            $list.empty();
            if (!orders || orders.length === 0) {
                $('#noOrders').show();
                return;
            }
            $('#noOrders').hide();
            orders.forEach(function(order) {
                var reviewHtml = '';
                if (order.review) {
                    var filledStars = Array(order.review.rating).fill('<span class="star-filled">★</span>').join('');
                    var emptyStars = Array(5-order.review.rating).fill('<span class="star-empty">☆</span>').join('');
                    reviewHtml = `<div class="order-review">
                        <div class="review-stars">
                            ${filledStars}${emptyStars}
                        </div>
                        <div class="review-content-text">${order.review.content}</div>
                        <div class="review-appends" id="appends_${order.orderId}"></div>
                    </div>`;
                }
                var html = `<div class="order-card">
                    <div class="order-header">订单编号：${order.orderId || ''}</div>
                    <div class="order-info">房型：${order.roomTypeName || ''}</div>
                    <div class="order-info">入住时间：${order.checkInTime || ''}</div>
                    <div class="order-info">联系人：${order.username || ''}，电话：${order.contactPhone || ''}</div>
                    <div class="order-info">总金额：¥${order.totalAmount ? order.totalAmount.toFixed(2) : '0.00'}</div>
                    <div class="order-status">状态：${order.status || '已预订'}</div>
                    <div class="order-actions">
                        <button class="review-btn" onclick="openReviewModal('${order.orderId}')" ${order.review ? 'disabled' : ''}>
                            ${order.review ? '已评价' : '评价订单'}
                        </button>
                    </div>
                    ${reviewHtml}
                </div>`;
                $list.append(html);
                // 查询追评并插入
                if(order.review) {
                    $.get('http://localhost:8080/api/order/appendReview/list', {orderId: order.orderId}, function(appends) {
                        if(appends && appends.length > 0) {
                            let appendHtml = '<div class="append-title">追评：</div>';
                            appends.forEach(function(a) {
                                appendHtml += `<div class="append-item">${a.appendText} <span class="append-time">${a.appendTime ? a.appendTime.replace('T',' ').substring(0,16) : ''}</span></div>`;
                            });
                            $('#appends_' + order.orderId).html(appendHtml);
                        }
                    });
                }
            });
        }
        // 评价相关变量
        var currentOrderId = null;
        var currentRating = 0;
        var isRatingLocked = false;
        
        // 打开评价对话框
        function openReviewModal(orderId) {
            currentOrderId = orderId;
            currentRating = 0;
            $('.star').removeClass('active');
            $('#reviewText').val('');
            $('#reviewModal').show();
        }
        
        // 关闭评价对话框
        function closeReviewModal() {
            $('#reviewModal').hide();
        }
        
        // 设置星级
        function setRating(rating) {
            if (!isRatingLocked) {
                currentRating = rating;
                isRatingLocked = true;
                $('.star').removeClass('active');
                for (var i = 1; i <= currentRating; i++) {
                    $('.star[data-rating="' + i + '"]').addClass('active');
                }
            } else {
                // 已锁定，再次点击任意星星解锁
                isRatingLocked = false;
                currentRating = 0;
                $('.star').removeClass('active');
            }
        }
        
        // 星星悬停预览效果
        $(document).on('mouseenter', '.star', function() {
            if (isRatingLocked) return;
            var rating = parseInt($(this).data('rating'));
            $('.star').removeClass('active');
            for (var i = 1; i <= rating; i++) {
                $('.star[data-rating="' + i + '"]').addClass('active');
            }
        });
        
        $(document).on('mouseleave', '#reviewModal', function() {
            if (isRatingLocked) return;
            $('.star').removeClass('active');
            for (var i = 1; i <= currentRating; i++) {
                $('.star[data-rating="' + i + '"]').addClass('active');
            }
        });
        
        // 提交评价
        function submitReview() {
            if (currentRating === 0) {
                alert('请选择星级评分');
                return;
            }
            var content = $('#reviewText').val().trim();
            if (!content) {
                alert('请填写评价内容');
                return;
            }
            
            $.ajax({
                url: 'http://localhost:8080/api/order/review',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    orderId: currentOrderId,
                    rating: currentRating,
                    content: content
                }),
                success: function(res) {
                    if (res === 'success') {
                        alert('评价提交成功！');
                        closeReviewModal();
                        // 重新加载订单列表
                        loadOrders();
                    } else {
                        alert('评价提交失败，请重试');
                    }
                },
                error: function() {
                    alert('网络错误，请重试');
                }
            });
        }
        
        // 重新加载订单
        function loadOrders() {
            $.ajax({
                url: 'http://localhost:8080/api/order/list?userId=' + userId,
                type: 'GET',
                success: function(data) {
                    renderOrders(data);
                },
                error: function() {
                    $('#ordersList').html('<div class="no-orders">订单加载失败，请稍后重试</div>');
                }
            });
        }
        // 请求订单数据
        loadOrders();
        
        // 点击模态框外部关闭
        $(document).on('click', '#reviewModal', function(e) {
            if (e.target.id === 'reviewModal') {
                closeReviewModal();
            }
        });
    </script>
</body>
</html> 